﻿var J_sale_all = $("#J_sale_all"); //可售数量
var J_sale_pernum = $("#J_sale_pernum"); //每件多少个
var J_sale_price = $("#J_sale_price"); //单价
var J_sale_perprice = $("#J_sale_perprice"); //单件售价
var J_sale_name = $("#J_sale_name"); //产品名称
var J_sale_num = $("#J_sale_num"); //售多少件
var J_sale_nummax = $("#J_sale_nummax"); //可售多少件
var J_sale_type = $("#J_sale_type"); //传奇世界
var J_sale_producttype = $("#J_sale_producttype"); //元宝.点券
var J_sale_priceunit = $("#J_sale_priceunit"); //货币单位
var J_storename = $("#J_storename"); //店铺名

$(document).ready(function () {
    var J_ekey = $("#J_ekey");
    var J_ekey_error = $("#J_ekey_error");
    //密宝
    if (J_ekey.length > 0) {
        J_ekey.show();
        J_ekey.find(".light-bg").css("height", $(document).height() + 20);
        J_ekey.find(".light-box").css({ left: ($(window).width() - J_ekey.find(".light-box").width()) / 2, top: ($(window).height() - J_ekey.find(".light-box").height()) / 2 });
        J_ekey.find(".light-iframe").css("height", $(document).height());
    };
    J_ekey.find("input:visible").keydown(function () {
        var t = $(this);
        var len = J_ekey.find("input:visible").length;
        var val = t.val().replace(/(^\s*)|(\s*$)/g, "");
        if (len > 2) {
            if (val.length == 1) {
                t.focus();
                return;
            };
        };

    }).keyup(function () {
        //格式
        var t = $(this);
        var len = J_ekey.find("input:visible").length;
        var val = t.val().replace(/(^\s*)|(\s*$)/g, "");
        var reg = /[^\d]$/;
        if (val.length > 0) {
            val = val.replace(reg, "");
        };
        if (len > 2) {
            if (val.length > 1) {
                val = val.substring(0, 1);
            };
            t.val(val);
            if (val.length != 1) {
                return;
            };
        }
        t.val(val);
        J_ekey_error.removeClass("form-showerror");
        t.next("input").focus();
    }).focus(function () {
        var t = $(this);
        t.select().removeClass("onerror").addClass("onfocus");
    }).blur(function () {
        var t = $(this);
        var len = J_ekey.find("input:visible").length;
        t.removeClass("onfocus");
        var val = t.val().replace(/(^\s*)|(\s*$)/g, "");
        if ((len > 2 && val.length != 1) || val.length < 1) {
            t.addClass("onerror");
            J_ekey_error.addClass("form-showerror");
            return;
        }
    });
    J_ekey.find("form").submit(function () {
        var parm = { "random": Math.random };
        var inputhidden = J_ekey.find("input:hidden");
        var inputhiddenlen = inputhidden.length;
        for (var i = 0; i < inputhiddenlen; i++) {
            parm[inputhidden.eq(i).attr("name")] = inputhidden.eq(i).val();
        };
        var input = J_ekey.find("input:visible");
        var text = "";
        var len = input.length;
        if (len > 1) {
            name = input.eq(0).attr("name");
        } else {
            name = input.attr("name");
        }
        for (var i = 0; i < len; i++) {
            input.eq(i).focus().blur();
            text += input.eq(i).val();
        };
        if (J_ekey.find("input.onerror").length > 0) {
            J_ekey_error.addClass("form-showerror");
            return false;
        };
        parm[name] = text;
        var url = $(this).attr("action");
        //加载数据
        $.ajax({
            async: true,
            url: url,
            data: parm,
            type: "POST",
            dataType: "text",
            success: function (msg) {
                var data = eval("(" + msg + ")");
                var status = data["status"];
                if (status == "1") {
                    J_ekey.hide();
                    return false;
                }
                J_ekey_error.addClass("form-showerror");
                return false;
            },
            error: function () {
                alert("抱歉，出错了！请刷新页面后再试。");
                return false;
            }
        });
        return false;
    });

    var J_light = $("#J_light");
    var J_form = $("#J_form");
    J_form.find("button[type='submit']").click(function () {
        this.blur();
    });
    //样式
    J_form.find("input[type='text']").focus(function () {
        $(this).addClass("form-text-onfocus");
    }).blur(function () {
        $(this).removeClass("form-text-onfocus");
    });

    //验证码
    var J_form_captcha = $("#J_form_captcha");
    J_form_captcha.find("img,span").click(function () {
        var url = J_form_captcha.attr("data-url-img") + "?random=" + Math.random();
        J_form_captcha.find("img").attr("src", url);
    }).click();
    J_form_captcha.find("input[type='text']").blur(function () {
        var t = $(this);
        var name = (!t.attr("name")) ? "captcha" : t.attr("name");
        var val = t.val();
        if (val.length < 1) {
            t.nextAll(".form-message").removeClass("form-showsuccess").addClass("form-showerror");
            return;
        };
        var url = J_form_captcha.attr("data-url-validator");
        var parm = { "random": Math.random };
        parm[name] = t.val();
        $.ajax({
            async: true,
            type: "POST",
            url: url,
            data: parm,
            dataType: "text",
            success: function (msg) {
                var data = msg;
                if (data == "true") {
                    t.nextAll(".form-message").removeClass("form-showerror").addClass("form-showsuccess");
                    return;
                }
                t.nextAll(".form-message").removeClass("form-showsuccess").addClass("form-showerror");
                J_form_captcha.find("img").click();
            },
            error: function () {
                alert("抱歉，出错了！请刷新页面后再试。");
            }
        });
    });

    //验证店铺名
    $("#J_storename").blur(function () {
        var t = $(this);
        if (t.val().length < 1) {
            t.nextAll(".form-message").removeClass("form-showsuccess").addClass("form-showerror").find(".error span").html("店铺名太短");
            return;
        };
        var maxlength = parseInt(t.attr("data-maxlength"));
        maxlength = isNaN(maxlength) ? 0 : maxlength;
        if (maxlength > 0 && t.val().length > maxlength) {
            t.nextAll(".form-message").removeClass("form-showsuccess").addClass("form-showerror").find(".error span").html("店铺名太长");
            return;
        };
        t.nextAll(".form-message").find(".error span").html("店铺名称格式不正确");
        var url = t.attr("data-verify");
        var parm = { "random": Math.random() };
        parm[t.attr("name")] = t.val();
        //加载数据
        $.ajax({
            async: true,
            url: url,
            data: parm,
            type: "POST",
            dataType: "text",
            success: function (msg) {
                var data = eval("(" + msg + ")");
                var status = data["status"];
                var message = data["message"];
                var shopId = data["shopId"];
                if (status == "0") {
                    $("#shopId").value = shopId;
                    $("#J_storename").next(".form-message").removeClass("form-showerror").addClass("form-showsuccess");
                    t.attr("disabled", "disabled").addClass("form-text-disabled");
                    return;
                }
                $("#J_storename").next(".form-message").removeClass("form-showsuccess").addClass("form-showerror");
            },
            error: function () {
                alert("抱歉，出错了！请刷新页面后再试。");
            }
        });
    }).keyup(function () {
        var t = $(this);
        //显示单价和出售产品名
        sumproduct();
        //长度报错
        if (t.val().length < 1) {
            t.nextAll(".form-message").removeClass("form-showsuccess").addClass("form-showerror").find(".error span").html("店铺名太短");
            return;
        };
        var maxlength = parseInt(t.attr("data-maxlength"));
        maxlength = isNaN(maxlength) ? 0 : maxlength;
        if (maxlength > 0 && t.val().length > maxlength) {
            t.nextAll(".form-message").removeClass("form-showsuccess").addClass("form-showerror").find(".error span").html("店铺名太长");
            return;
        };
        t.nextAll(".form-message").removeClass("form-showsuccess").removeClass("form-showerror");
    });
    //验证每件多少个元宝/点券
    J_sale_pernum.keyup(function () {
        //格式
        var t = $(this);
        var val = t.val().replace(/(^\s*)|(\s*$)/g, "");
        var reg = /[^\d]$/;
        if (val.length > 0) {
            val = val.replace(reg, "");
            t.val(val);
        };
        var gameCurrency = parseInt(J_sale_producttype.val());
        //判定数量
        var all = parseInt(J_sale_all.html());
        all = isNaN(all) ? 0 : all;
        var per = parseInt(t.val());
        per = (isNaN(per)) ? 0 : per;
        //最大件数
        var nummax = (per < 1) ? 0 : Math.floor(all / per);
        J_sale_num.nextAll(".form-message").removeClass("hidden");
        J_sale_nummax.html(nummax);
        //显示单价和出售产品名
        sumproduct();
        //可售件数
        if (gameCurrency == 2 && (per % 100 != 0)) {
            t.nextAll(".form-message").removeClass("form-showsuccess").addClass("form-showerror").find(".error span").html("数量必须为100的整数倍");
            return;
        };
        //报错
        if (all > 0 && per > all) {
            t.nextAll(".form-message").removeClass("form-showsuccess").addClass("form-showerror").find(".error span").html("数量不能大于可售总数");
            return;
        };
        if (per < 1) {
            t.nextAll(".form-message").removeClass("form-showsuccess").addClass("form-showerror").find(".error span").html("单件数量不能小于1");
            return;
        };
        t.nextAll(".form-message").removeClass("form-showerror").addClass("form-showsuccess");
        return;
    }).blur(function () {
        var t = $(this);
        if (t.val().length < 1) {
            t.nextAll(".form-message").removeClass("form-showsuccess").addClass("form-showerror");
            return;
        };
        t.keyup();
    });

    //单价
    J_sale_price.keyup(function () {
        //格式
        var t = $(this);
        var val = t.val().replace(/(^\s*)|(\s*$)/g, "");
        if (!(/^(?:\d+|\d+\.\d{0,4})$/.test(val))) {
            val = val.replace(/^(\d*\.\d{0,4}|\d+).*$/, '$1');
            t.val(val);
        };
        //显示单价和出售产品名
        sumproduct();
        //报错
        var minprice = parseFloat(J_sale_priceunit.attr("data-minprice"));
        minprice = isNaN(minprice) ? 0 : minprice;
        var maxprice = parseFloat(J_sale_priceunit.attr("data-maxprice"));
        maxprice = isNaN(maxprice) ? 0 : maxprice;
        if (val < 0.0001) {
            t.nextAll(".form-message").removeClass("form-showsuccess").addClass("form-showerror").find(".error span").html("价格错误");
            return;
        };
        if ((minprice > 0 && val < minprice) || (maxprice > 0 && val > maxprice)) {
            t.nextAll(".form-message").removeClass("form-showsuccess").addClass("form-showerror").find(".error span").html("请输入商品单价，且控制在" + minprice + "至" + maxprice + "之间，精确到四位小数");
            return;
        };
        t.nextAll(".form-message").removeClass("form-showerror").addClass("form-showsuccess");
        return;
    }).blur(function () {
        var t = $(this);
        var num = parseFloat(t.val());
        num = isNaN(num) ? 0 : num;
        if (t.val().length < 1 || num < 0.0001) {
            t.nextAll(".form-message").removeClass("form-showsuccess").addClass("form-showerror").find(".error span").html("价格错误");
            return;
        };
        t.keyup();
    });

    //可售多少件
    J_sale_num.keyup(function () {
        //格式
        var t = $(this);
        var val = t.val().replace(/(^\s*)|(\s*$)/g, "");
        var reg = /[^\d]$/;
        if (val.length > 0) {
            val = val.replace(reg, "");
            t.val(val);
        };
        //判定数量
        var all = parseInt(J_sale_nummax.html());
        if (isNaN(all)) { all = 0 };
        var per = parseInt(t.val());
        if (isNaN(per)) { per = 0 };
        if (per > all || per < 1) {
            t.nextAll(".form-message").removeClass("form-showsuccess").addClass("form-showerror");
            return;
        };
        t.nextAll(".form-message").removeClass("form-showerror").addClass("form-showsuccess");
        return;
    }).blur(function () {
        var t = $(this);
        if (t.val().length < 1) {
            t.nextAll(".form-message").removeClass("form-showsuccess").addClass("form-showerror");
            return;
        };
        t.keyup();
    });

    //初始化表单状态
    $("#J_form_check").val("0");
    //提交信息
    J_form.submit(function () {
        var t = $(this);
        //如果检查通过，直接提交
        if ($("#J_form_check").val() == "1") { return true; }
        //验证填写信息
        var fields = t.find("input:visible");
        var count = fields.length;
        for (var i = 0; i < count; i++) {
            field = fields.eq(i);
            if (field.nextAll(".form-showsuccess").length < 1) {
                field.focus();
                field.blur();
            }
        };
        if (t.find(".form-showerror").length > 0) {
            return false;
        };
        //高亮检查
        var light = $("#J_light");
        light.show();
        light.find(".light-bg").css("height", $(document).height());
        light.find(".light-box").css({ left: ($("#J_form").offset().left + 200), top: $("#J_form").offset().top + 50 });
        light.find(".light-iframe").css("height", $(document).height());
        var html = "<table>";
        var label = t.find("[data-tolight='label']");
        var content = t.find("[data-tolight='content']");
        var count = label.length;
        for (var i = 0; i < count; i++) {
            var a = "<tr><td class='label'>" + label.eq(i).html().replace(/<[^>]*>/ig, "").replace("*", "") + "</td><td>";
            var c = content.eq(i);
            if (c.find("input[type='radio']").length > 0) {
                a += c.find("input[type='radio']:checked").attr("title");
            } else if (c.find("input[type='text']").length > 0) {
                var ct = c.find("input[type='text']");
                var num = ct.length;
                for (var j = 0; j < num; j++) {
                    a += ct.eq(j).val();
                    if (ct.eq(j).next("span[class='form-tip']").length > 0) { a += ct.eq(j).next("span[class='form-tip']").html(); }
                }
            } else if (c.find("select").length > 0) {
                var ct = c.find("select");
                var num = ct.length;
                for (var j = 0; j < num; j++) {
                    a += ct.eq(j).find("option:selected").text() + "/";
                };
                a = a.substring(0, a.length - 1);
            } else {
                a += c.html();
            }
            a += "</td></tr>" ;
            html += a;
        };
        html += "</table>";
        light.find(".light-form").html(html);
        return false;
    });
    J_light.find("button[type='submit']").click(function () {
        $("#J_form_check").val("1");
        J_form.submit();
    });
    J_light.find("button[type='reset']").click(function () {
        J_light.hide();
    });
    J_light.find(".light-close").click(function () {
        J_light.hide();
    });
});

//计算单价
function sumper() {
    var html = "";
    var pernum = parseInt(J_sale_pernum.val());
    var gamecurrency = parseInt(J_sale_producttype.val());
    var storename = J_storename.attr("type") == "text" ? J_storename.val() : J_storename.html();
    html += isNaN(pernum) ? "&nbsp;" : pernum + J_sale_type.val() + J_sale_producttype.attr("title") + "=";
    if (gamecurrency == 2) {pernum = pernum / 100;  };
    var sale_price = FloatMul(pernum, parseFloat(J_sale_price.val()));
    //价格取2位小数
    var re = /([0-9]+\.[0-9]{2})[0-9]*/;
    html += isNaN(sale_price) ? "" : (""+sale_price).replace(re,"$1") + J_sale_priceunit.val();
    J_sale_perprice.html(html);
    return html;
}
//商品名称
function sumproduct() {
    var html = "";
    var storename = J_storename.attr("type") == "text" ? J_storename.val() : J_storename.html();
    html += "【" + storename + "】" + "&nbsp;";
    html += sumper();
    J_sale_name.html(html);
};
//精确乘法计算
function FloatMul(arg1, arg2) {
    var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
    try { m += s1.split(".")[1].length } catch (e) { }
    try { m += s2.split(".")[1].length } catch (e) { }
    return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
}